package com.pipe.protocol.login;

import com.pipe.util.IntegerConveter;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * @author Kiven
 */
public class ComRegisterSlavePackage {
    private byte comRegisterSlave = 0x15;
    private byte[] slaveServerId;
    private byte slavesHostnameLength;
    private byte[] slavesHostname;
    private byte slaveUserLen;
    private byte[] slavesUser;
    private byte slavePasswordLen;
    private byte[] slavePassword;
    private byte[] slavesMysqlPort;
    private byte replicationRank;
    private byte masterId;

    public ComRegisterSlavePackage(byte[] slaveServerId) {
        this.slaveServerId = slaveServerId;
    }

    public static ComRegisterSlavePackage build(int slaveServerId){
        return new ComRegisterSlavePackage(IntegerConveter.getByte4Array(slaveServerId));
    }

    public ByteBuf toBytebuf(){
        byte[] arr = new byte[]{comRegisterSlave, 0, 0, 0, 0, slavesHostnameLength, slaveUserLen, slavePasswordLen, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        System.arraycopy(slaveServerId, 0, arr, 1, 4);
        ByteBuf byteBuf = Unpooled.directBuffer(arr.length);
        byteBuf.writeBytes(arr);
        return byteBuf;
    }

}